package com.util.logging;


/**
 * 官方教程 https://docs.oracle.com/javase/8/docs/technotes/guides/logging
 * /overview.html#a2.1
 */

import org.junit.Test;

import java.io.IOException;
import java.util.logging.*;

public class LoggerDemo {
    // Obtain a suitable logger.
    private static Logger logger = Logger.getLogger(LoggerDemo.class.getName());

    public static void main(String argv[]) {
        // Log a FINE tracing message
        logger.fine("doing stuff");
        try {
            logger.fine("iniii");
            logger.log(Level.INFO, "hhhh");
        } catch (Exception ex) {
            // Log the exception
            logger.log(Level.WARNING, "trouble sneezing", ex);
        }
        logger.fine("done");
    }


    @Test
    public void testHandler() throws IOException {
        Handler fh = new FileHandler("%t/logger.log");
        Logger.getLogger("").addHandler(fh);
        Logger.getLogger("com.util.logging").setLevel(Level.FINEST);
        Logger.getLogger("").info("aaa");
    }


    /**
     * 会生成一个xml文件 /home/away/IdeaProjects/javastudy/mylog.txt
     *
     * <record>
     * <date>2019-01-06T22:36:15</date>
     * <millis>1546785375105</millis>
     * <sequence>0</sequence>
     * <logger>com.wombat.nose</logger>
     * <level>INFO</level>
     * <class>com.util.logging.LoggerDemo</class>
     * <method>test01</method>
     * <thread>1</thread>
     * <message>doing stuff</message>
     * </record>
     *
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {

        Logger logger = Logger.getLogger("com.wombat.nose");//com.wombat.nose
        //  logger对象的名称
        FileHandler fh = new FileHandler("mylog.txt");
        // Send logger output to our FileHandler.
        logger.addHandler(fh);
        // Request that every detail gets logged.
        logger.setLevel(Level.ALL);
        // Log a simple INFO message.
        logger.info("doing stuff");
        try {
//            Wombat.sneeze();
        } catch (Exception ex) {
            logger.log(Level.WARNING, "trouble sneezing", ex);
        }
        logger.fine("done");
    }

    /**
     * 使用 SimpleFormatter
     */
    @Test
    public void test02() throws IOException {
//        java.util.logging.SimpleFormatter.format
//        %1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n
//        Jan 06, 2019 11:06:53 PM com.util.logging.LoggerDemo test02
//        信息: aaa

        /**
         * 这些参数用来 设置SimpleFormatter里面的format参数，貌似这个不能看到线程id
         */
//        System.setProperty("java.util.logging.SimpleFormatter.format","%1$s # %2$s # %3$s # %4$s # %5$s # %6$s%n");

//        System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tH:%1$tM:%1$tS %2$s %4$s: %5$s%6$s%n");
        System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n");

//        System.setProperty("java.util.logging.SimpleFormatter.format","%1$tF %1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n");//正常的时间格式
        Logger logger = Logger.getLogger("com.util.logging");//com.wombat.nose
        //  logger对象的名称
        FileHandler fh = new FileHandler("mylog.txt");
        SimpleFormatter sf = new SimpleFormatter();
//        sf.format()
        fh.setFormatter(sf);
        logger.addHandler(fh);
        logger.setLevel(Level.ALL);
        logger.info("aaa");
        logger.info("aaa");


    }
}

